/**
 * Created by Administrator on 2015/12/3.
 */
"use strict";
var F = require('../../../common/function');
var order_F = require('./order_common');
var _o2oServiceApi = require('../../../common/o2oServiceApi');
var o2oServiceApi = new _o2oServiceApi;
var _ = require('underscore');

module .exports.coupon_order_list = function*(queryObj){
    queryObj.act =1;
    if(!!queryObj.id){
        queryObj.vid =queryObj.id;
        delete queryObj.id;
    }
    if(!!queryObj.time){
        queryObj.time =queryObj.time;//将时间处理为时间戳
    }

    if(!!queryObj.ticket_code){
        queryObj.ticket_code =queryObj.ticket_code;
    }

    let ticketResult  = yield  o2oServiceApi.post('order','getTicket',1,queryObj);

    if(ticketResult.status == 400 || ticketResult['result']['error']==1){
            return F.returnMsg(300, ticketResult['message']['msg'], 1);
    }
    if(ticketResult['result']['data'].length ==0){
            return F.returnMsg(300, '没有数据了！', 1, ticketResult['result']['data']);
    }
    let returnData =[];
    let ids =[];
    for(let i of ticketResult['result']['data']){
        let tmpObj ={
            "ticket_code":i.ticket_code,
            "status":i.status,
            "consume_date":i.consume_date,
            "orderid":i.order_id
        };
        ids.push(i.order_id);
        returnData.push(tmpObj);
    }
    ids = _.uniq(ids);
    //获取订单详情
    let orderResult={};
    orderResult.result =[];
    for(let i of ids){
        let order = yield o2oServiceApi.post('order','detail',1,{"act":1,"orderid":i});
        if(order['result'] && !_.isEmpty(order['result'])){
            let obj ={"orderid":order['result']['orderid'],"uid":order['result']['uid'],"order_code":order['result']['order_code'],"wareName":order['result']['wareName']};
            orderResult.result.push(obj);
        }
    }
    yield order_F.addUserInfo(orderResult);
    for(let i of returnData){
        for(let j of orderResult.result){
            if(i.orderid == j.orderid){
                _.extend(i,j);
                if(queryObj.type ==2){delete  i.name;}else{delete i.mobile;}
                break;
            }
        }
    }
    //type==1时，需要机构信息

    return F.returnMsg(200, '查找成功', 1, returnData);

}